package toDo.app.models;

import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * this is an immutable data object, use the is valid method to check for
 * correct and such parcelable means you can pass it between activities via
 * intents
 * 
 * @author Sparky
 * 
 */
public final class User implements Parcelable {
	private final int id;
	private final String username;
	private final String password;
	private final boolean valid;

	public User(Parcel in) {
		this.username = in.readString();
		this.password = in.readString();
		this.id = in.readInt();
		boolean[] bools = new boolean[1];
		in.readBooleanArray(bools);
		this.valid = bools[0];
	}

	public User(String username, String password) {
		this.id = -1;
		this.password = password;
		this.username = username;
		this.valid = false;
	}

	public User(String username, String password, int id) {
		this.id = id;
		this.password = password;
		this.username = username;
		this.valid = true;
	}

	public boolean isValid() {
		return valid;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public int getId() {
		return id;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.getUsername());
		dest.writeString(this.getPassword());
		dest.writeInt(this.getId());
		dest.writeBooleanArray(new boolean[] { this.isValid() });
	}

	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
		@Override
		public User createFromParcel(Parcel in) {
			return new User(in);
		}

		@Override
		public User[] newArray(int size) {
			return new User[size];
		}
	};

	public static Intent putUserIntoIntent(Intent intent, User user) {
		intent.putExtra(User.class.getName(), user);
		return intent;
	}

	public static User getUserFromIntent(Intent intent) {
		if (intent.hasExtra(User.class.getName()))
			return (User) intent.getParcelableExtra(User.class.getName());
		return null;
	}
}
